﻿app.factory("skillfactory", function () {
    return [];
});
app.factory("projectfactory", function () {
    return [];
});

app.controller("ngMainController", function ($scope, $http, $window, $q, $location, skillfactory, projectfactory, $route, $interval) {
    $scope.skills = [];
    $scope.currentPage = 1;
    $scope.getSkills = function () {
        $http.get("/api/skill").success(function (data) {
            //skillfactory = [];
            $scope.filteredSkills = data;
            $scope.paginate(0);
            $scope.$watch("currentPage", function () {
                var begin = (($scope.currentPage - 1) * 4);
                console.log($scope.currentPage)
                $scope.skills = $scope.filteredSkills.slice(begin, begin + 4);
            })
            $scope.skills = $scope.filteredSkills.slice(0, 4);
        }).error(function (data) {
            console.log(JSON.stringify(data))
        });
    }
    $scope.paginate = function (number) {
        if (number === null) number = 0;
        $scope.currentPage += number;
        $scope.button = {prev:"",next:""}
        //if ($scope.currentPage === 1 && $scope.filteredSkills.length <= 4) {
        //    $scope.button = { prev: "disabled", next: "disabled" }
        //} else {
        //    $scope.button = { prev: "disabled", next: "enabled" }
        //}
        if ($scope.currentPage === 1) $scope.button.prev = "disabled";
        else $scope.button.prev = "enabled";
        if ($scope.currentPage * 4 < $scope.filteredSkills.length) $scope.button.next = "enabled";
        else $scope.button.next = "disabled";
    }
    $scope.getSkills();

    $scope.getProjects = function () {
        $http.get("/api/project").success(function (data) {
            //projectfactory = [];
            $scope.projects = data;
        }).error(function (data) {
            console.log(JSON.stringify(data))
        });
    }
    $scope.getProjects();

    $scope.AddSkill = function (skill) {
        $http.post("/api/skill", skill).success(function (data) {
            $scope.getSkills();
            skill = data;
            $scope.paginate(0);
        }).error(function (data) {
            console.log(data);
            alert("Error adding!")
            $location.path("/");
        });
    };

    $scope.AddProject = function (project) {
        $http.post("/api/project", project).success(function (data) {
            project = data;
            $window.location.reload();
        }).error(function (data) {
            console.log(data);
            alert("Error adding!")
            $location.path("/");
        });
    }
    $scope.editing = false;
    $scope.EditSkill = function (skill) {
        $http.put("api/skill/", skill).success(function (data) {
            $scope.getSkills();
            skill = data;
        }).error(function (data) {
            $scope.error = "Error, unable to save." + data;
        });
    };

    $scope.DeleteSkill = function (skill) {
        $http.delete("api/skill/" + skill.Id).success(function (data) {
            $scope.getSkills()
            if (4 * ($scope.currentPage - 1) === --($scope.filteredSkills.length)) {
                $scope.currentPage -= 1;
            }
            console.log($scope.filteredSkills.length)
        }).error(function (data) {
            $scope.error = "Error cannot delete!" + data;
        });
    };

    $scope.editingProject = false;
    $scope.EditProject = function (project) {
        $http.put("api/project/", project).success(function (data) {
            project = data;
            $window.location.reload();
        }).error(function (data) {
            $scope.error = "Error, unable to save." + data;
        });
    };

    $scope.DeleteProject = function (project) {
        $http.delete("api/project/" + project.Id).success(function (data) {
            $window.location.reload();
        }).error(function (data) {
            $scope.error = "Error cannot delete!" + data;
        });
    };
});